package com.fingard.dsp.bank.directbank.allinpay02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.HashMap;

/**
 * AllInPay02-通联单笔实时代扣
 */
public class AllInPay02RealTime extends AllInPay02Base implements IRealTime {
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9108);

        if ("9108Agrm".equalsIgnoreCase(p_req9108.reqHeader.commandCode) || "9188to9108Agrm".equalsIgnoreCase(p_req9108.reqHeader.commandCode)) {
            tmpRetDetail = d_9108_Agrm(p_req9108);
        } else if ("9108JFB".equals(p_req9108.reqHeader.commandCode)) {
            tmpRetDetail = d_9108JFB(p_req9108);
        } else {
            tmpRetDetail = d_9108_RealTime(p_req9108);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
        if ("9108JFB".equals(p_req9144.reqHeader.commandCode)) {
            return d_9144_9108JFB(p_req9144);
        } else {
            return d_9144_QryOrdinarySglPay(p_req9144);
        }
    }

    public RetAtsSglDetail d_9108_Agrm(ReqSingle p_req9108) throws Exception {
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9108);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9108.singleDetail.recAct + "]商户配置信息!";
                tmpRetDetail.transAmt = p_req9108.singleDetail.payAmount;
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                return tmpRetDetail;
            }

            AllInPay02Header header = new AllInPay02Header();
            header.TRX_CODE = "310011";
            header.REQ_SN = p_req9108.singleDetail.askNumber;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeAgrmCommonHeader(xmlWriter, header, actItem);

            XElement tmpXElement = new XElement("FASTTRX", "");
            String BUSINESS_CODE = this.getBankConfig().getMapAdvanceValue("BUSINESS_CODE_9108", "srcCode", "desCode",
                    p_req9108.singleDetail.useType);
            if (StringHelper.isNullOrEmpty(BUSINESS_CODE)) {
                BUSINESS_CODE = this.getBankConfig().getMapAdvanceValue("BUSINESS_CODE_9188", "srcCode", "desCode",
                        p_req9108.singleDetail.useType);
            }
            tmpXElement.addChild(new XElement("BUSINESS_CODE", BUSINESS_CODE));
            tmpXElement.addChild(new XElement("MERCHANT_ID", actItem.merchantId));
            tmpXElement.addChild(new XElement("SUBMIT_TIME", Format.DateTime14Format.format(p_req9108.singleDetail.getPayDateTime())));
            tmpXElement.addChild(new XElement("AGRMNO", p_req9108.singleDetail.authCode));
            tmpXElement.addChild(new XElement("ACCOUNT_NO", p_req9108.singleDetail.payAct));
            tmpXElement.addChild(new XElement("ACCOUNT_NAME", p_req9108.singleDetail.payName));
            tmpXElement.addChild(new XElement("AMOUNT", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount)));
            tmpXElement.addChild(new XElement("CURRENCY", convertCurToBank(p_req9108.singleDetail.payCur)));
            tmpXElement.addChild(new XElement("ID_TYPE", convertCertTypeToBank(p_req9108.singleDetail.certType)));
            tmpXElement.addChild(new XElement("ID", p_req9108.singleDetail.certNum));
            tmpXElement.addChild(new XElement("TEL", p_req9108.singleDetail.cellPhone));
            tmpXElement.addChild(new XElement("CVV2", p_req9108.singleDetail.cvv2));
            tmpXElement.addChild(new XElement("VAILDDATE", p_req9108.singleDetail.expiredDate));
            tmpXElement.addChild(new XElement("CUST_USERID", p_req9108.singleDetail.askNumber));
            tmpXElement.addChild(new XElement("SUMMARY", p_req9108.singleDetail.abstractATS));
            tmpXElement.addChild(new XElement("REMARK", p_req9108.singleDetail.memo));
            nodeAnalysis(p_req9108.singleDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendContractToBank(xmlWriter.toXmlString(), actItem);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String INFO_RET_CODE = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
                    String INFO_ERR_MSG = tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG");
                    String tmpBkRET_CODE = tmpBkReader.getFirstText("/AIPG/FASTTRXRET/RET_CODE");
                    String tmpBkERR_MSG = tmpBkReader.getFirstText("/AIPG/FASTTRXRET/ERR_MSG");

                    if (p_req9108.singleDetail.askNumber.equals(tmpBkReader.getSimpleText("REQ_SN"))) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.reqSeqID = tmpBkReader.getSimpleText("REQ_SN");
                        tmpRetDetail.transState = "H+" + INFO_RET_CODE;
                        if (tmpBkRET_CODE != null && tmpBkRET_CODE.length() > 0) {
                            tmpRetDetail.transState += "+D+" + tmpBkRET_CODE;
                            tmpRetDetail.payInfoCode = tmpBkRET_CODE;
                            if ("3999".equals(tmpBkRET_CODE)) {
                                tmpRetDetail.payInfoCode = tmpBkERR_MSG;
                            }
                            tmpRetDetail.respInfo = tmpBkERR_MSG;
                        } else {
                            tmpRetDetail.payInfoCode = INFO_RET_CODE;
                            tmpRetDetail.respInfo = INFO_ERR_MSG;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "请求流水号" + tmpRetDetail.reqSeqID + "与响应" + tmpBkReader.getSimpleText("REQ_SN") + "不一致";
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1];
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Batch;
                tmpRetDetail.payInfoCode = tmpStrRet[0];
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return tmpRetDetail;
    }

    public RetAtsSglDetail d_9108_RealTime(ReqSingle p_req9108) throws Exception {
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9108);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9108.singleDetail.recAct + "]商户配置信息!";
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                return tmpRetDetail;
            }

            AllInPay02Header header = new AllInPay02Header();
            header.TRX_CODE = "100011";
            header.REQ_SN = p_req9108.singleDetail.askNumber;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header, actItem);

            XElement tmpXElement = new XElement("TRANS", "");
            String BUSINESS_CODE = this.getBankConfig().getMapAdvanceValue("BUSINESS_CODE_9108", "srcCode", "desCode",
                    p_req9108.singleDetail.useType);
            if (StringHelper.isNullOrEmpty(BUSINESS_CODE)) {
                BUSINESS_CODE = this.getBankConfig().getMapAdvanceValue("BUSINESS_CODE_9188", "srcCode", "desCode",
                        p_req9108.singleDetail.useType);
            }
            tmpXElement.addChild(new XElement("BUSINESS_CODE", BUSINESS_CODE));
            tmpXElement.addChild(new XElement("MERCHANT_ID", actItem.merchantId));
            tmpXElement.addChild(new XElement("SUBMIT_TIME", Format.DateTime14Format.format(p_req9108.singleDetail.getPayDateTime())));
            String[] findValues = new String[2];
            findValues[0] = p_req9108.singleDetail.payBankCode;
            findValues[1] = p_req9108.singleDetail.payAreaName;
            HashMap<String, String> BANK_CODE = this.getBankConfig().getMapAdvanceValues("BankCodeMapping",
                    new String[]{"ATSBankCode", "ATSAreaName"}, new String[]{"value"}, findValues);
            if (BANK_CODE != null && BANK_CODE.size() > 0 && BANK_CODE.containsKey("value")) {
                tmpXElement.addChild(new XElement("BANK_CODE", BANK_CODE.get("value")));
            } else {
                tmpXElement.addChild(new XElement("BANK_CODE", findValues[0]));
            }
            if (p_req9108.singleDetail.cardType.equals("1")) {
                tmpXElement.addChild(new XElement("ACCOUNT_TYPE", "01"));
            } else if (p_req9108.singleDetail.cardType.equals("3")) {
                tmpXElement.addChild(new XElement("ACCOUNT_TYPE", "02"));
            } else {
                tmpXElement.addChild(new XElement("ACCOUNT_TYPE", "00"));
            }
            tmpXElement.addChild(new XElement("ACCOUNT_NO", p_req9108.singleDetail.payAct));
            tmpXElement.addChild(new XElement("ACCOUNT_NAME", p_req9108.singleDetail.payName));
            if (p_req9108.singleDetail.isPrivate.equals("1")) {
                tmpXElement.addChild(new XElement("ACCOUNT_PROP", "0"));
            } else {
                tmpXElement.addChild(new XElement("ACCOUNT_PROP", "1"));
            }
            tmpXElement.addChild(new XElement("AMOUNT", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount)));
            tmpXElement.addChild(new XElement("CURRENCY", convertCurToBank(p_req9108.singleDetail.payCur)));
            tmpXElement.addChild(new XElement("ID_TYPE", convertCertTypeToBank(p_req9108.singleDetail.certType)));
            tmpXElement.addChild(new XElement("ID", p_req9108.singleDetail.certNum));
            tmpXElement.addChild(new XElement("REMARK", p_req9108.singleDetail.memo));
            nodeAnalysis(p_req9108.singleDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), actItem);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String INFO_RET_CODE = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
                    String INFO_ERR_MSG = tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG");
                    String tmpBkRET_CODE = tmpBkReader.getFirstText("/AIPG/TRANSRET/RET_CODE");
                    String tmpBkERR_MSG = tmpBkReader.getFirstText("/AIPG/TRANSRET/ERR_MSG");
                    if (p_req9108.singleDetail.askNumber.equals(tmpBkReader.getSimpleText("REQ_SN"))) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.reqSeqID = tmpBkReader.getSimpleText("REQ_SN");
                        tmpRetDetail.transState = "H+" + INFO_RET_CODE;
                        if (tmpBkRET_CODE != null && tmpBkRET_CODE.length() > 0) {
                            tmpRetDetail.transState += "+D+" + tmpBkRET_CODE;
                            tmpRetDetail.payInfoCode = tmpBkRET_CODE;
                            if ("3999".equals(tmpBkRET_CODE)) {
                                tmpRetDetail.payInfoCode = tmpBkERR_MSG;
                            }
                            tmpRetDetail.respInfo = tmpBkERR_MSG;
                        } else {
                            tmpRetDetail.payInfoCode = INFO_RET_CODE;
                            tmpRetDetail.respInfo = INFO_ERR_MSG;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "请求流水号" + tmpRetDetail.reqSeqID + "与响应" + tmpBkReader.getSimpleText("REQ_SN") + "不一致";
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1];
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Batch;
                tmpRetDetail.payInfoCode = tmpStrRet[0];
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return tmpRetDetail;
    }

    public RetAtsSglDetail d_9108JFB(ReqSingle p_req9108) throws Exception {
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9108);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9108.singleDetail.recAct + "]商户配置信息!";
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                return tmpRetDetail;
            }

            AllInPay02Header header = new AllInPay02Header();
            header.TRX_CODE = "100011";
            header.REQ_SN = p_req9108.singleDetail.askNumber;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeJFBCommonHeader(xmlWriter, header, actItem);

            xmlWriter.writeStartElement("BODY");
            XElement tmpXElement = new XElement("TRANS", "");
            String BUSINESS_CODE = this.getBankConfig().getMapAdvanceValue("BUSINESS_CODE_9108", "srcCode", "desCode",
                    p_req9108.singleDetail.useType);
            if (StringHelper.isNullOrEmpty(BUSINESS_CODE)) {
                BUSINESS_CODE = this.getBankConfig().getMapAdvanceValue("BUSINESS_CODE_9188", "srcCode", "desCode",
                        p_req9108.singleDetail.useType);
            }
            tmpXElement.addChild(new XElement("BUSINESS_CODE", BUSINESS_CODE));
            if (p_req9108.singleDetail.cardType.equals("1")) {
                tmpXElement.addChild(new XElement("ACCOUNT_TYPE", "01"));
            } else if (p_req9108.singleDetail.cardType.equals("3")) {
                tmpXElement.addChild(new XElement("ACCOUNT_TYPE", "02"));
            } else {
                tmpXElement.addChild(new XElement("ACCOUNT_TYPE", "00"));
            }
            if (p_req9108.singleDetail.isPrivate.equals("1")) {
                tmpXElement.addChild(new XElement("ACCOUNT_PROP", "0"));
            } else {
                tmpXElement.addChild(new XElement("ACCOUNT_PROP", "1"));
            }
            tmpXElement.addChild(new XElement("ACCOUNT_NO", p_req9108.singleDetail.payAct));
            tmpXElement.addChild(new XElement("ACCOUNT_NAME", p_req9108.singleDetail.payName));
            tmpXElement.addChild(new XElement("CVV2", ""));
            tmpXElement.addChild(new XElement("VALIDATE", ""));
            tmpXElement.addChild(new XElement("BANK_CODE", getBankConfig().getMapValueByKey("contractBankMapping", p_req9108.singleDetail.payBankCode)));
            tmpXElement.addChild(new XElement("AMOUNT", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount)));
            tmpXElement.addChild(new XElement("CURRENCY", convertCurToBank(p_req9108.singleDetail.payCur)));
            String ID_TYPE = "";
            String ID = "";
            String ifNeed = getBankConfig().getMapValueByKey("needCertBank", p_req9108.singleDetail.payBankCode);
            if (ifNeed.toLowerCase().equals("true")) {
                String tmpMapped_ID_TYPE = this.convertCertTypeToBank(p_req9108.singleDetail.certType);
                if (StringHelper.hasAnyChar(tmpMapped_ID_TYPE) && StringHelper.hasAnyChar(p_req9108.singleDetail.certNum)) {
                    ID_TYPE = tmpMapped_ID_TYPE;
                    ID = p_req9108.singleDetail.certNum;
                }
            }
            tmpXElement.addChild(new XElement("ID_TYPE", ID_TYPE));
            tmpXElement.addChild(new XElement("ID", ID));
            tmpXElement.addChild(new XElement("TEL", p_req9108.singleDetail.cellPhone));
            tmpXElement.addChild(new XElement("CUST_USERID", p_req9108.singleDetail.askNumber));
            tmpXElement.addChild(new XElement("SUMMARY", p_req9108.singleDetail.purpose));
            tmpXElement.addChild(new XElement("REMARK", p_req9108.singleDetail.memo));

            nodeAnalysis(p_req9108.singleDetail.srcXElement, tmpXElement, getCharset());
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendJFBToBank(xmlWriter.toXmlString(), actItem, header.REQ_SN);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String retCode = tmpBkReader.getFirstText("/AIPG/HEAD/RET_CODE");
                    String retMsg = tmpBkReader.getFirstText("/AIPG/HEAD/RET_MSG");
                    if (p_req9108.singleDetail.askNumber.equals(tmpBkReader.getSimpleText("REQ_SN"))) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.reqSeqID = tmpBkReader.getSimpleText("REQ_SN");
                        tmpRetDetail.transState = "JH+" + retCode;
                        tmpRetDetail.payInfoCode = retCode;
                        tmpRetDetail.respInfo = retMsg;
                        if ("3999".equals(retCode)) {
                            tmpRetDetail.payInfoCode = retMsg;
                        }
                        tmpRetDetail.transAmt = p_req9108.singleDetail.payAmount;
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = "请求流水号" + tmpRetDetail.reqSeqID + "与响应流水号" + tmpBkReader.getSimpleText("REQ_SN") + "不一致";
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1];
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Batch;
                tmpRetDetail.payInfoCode = tmpStrRet[0];
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return tmpRetDetail;
    }

    public RetAtsSingle d_9144_QryOrdinarySglPay(ReqQrySingle p_req9144) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9144);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9144.recAct, "");
            if (actItem != null) {
                AllInPay02Header bankHeader = new AllInPay02Header();
                bankHeader.REQ_SN = getQryTransID();
                bankHeader.TRX_CODE = "200004";

                XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
                writeCommonHeader(xmlWriter, bankHeader, actItem);// 写入通用的头部节点
                xmlWriter.writeStartElement("QTRANSREQ");
                xmlWriter.writeElementString("MERCHANT_ID", actItem.merchantId);
                xmlWriter.writeElementString("QUERY_SN", p_req9144.askNumber);
                xmlWriter.writeElementString("STATUS", "2");
                xmlWriter.writeElementString("TYPE", "1");
                xmlWriter.writeElementClosed("START_DAY");
                xmlWriter.writeElementClosed("END_DAY");
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = new String[]{"", ""};
                if ("9108Agrm".equalsIgnoreCase(p_req9144.reqHeader.commandCode) || "9188to9108Agrm".equalsIgnoreCase(p_req9144.reqHeader.commandCode)) {
                    tmpStrRet = sendContractToBank(xmlWriter.toXmlString(), actItem);
                } else {
                    tmpStrRet = sendToBank(xmlWriter.toXmlString(), actItem);
                }

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String INFO_RET_CODE = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
                        String INFO_ERR_MSG = tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG");
                        if (INFO_RET_CODE.equals("0000") || INFO_RET_CODE.equals("0001") || INFO_RET_CODE.equals("0002") || INFO_RET_CODE.equals("1002") || INFO_RET_CODE.equals("1999") || INFO_RET_CODE.equals("2000") || INFO_RET_CODE.equals("2001") || INFO_RET_CODE.equals("2002") || INFO_RET_CODE.equals("2003") || INFO_RET_CODE.equals("2004") || INFO_RET_CODE.equals("2005") || INFO_RET_CODE.equals("2006") || INFO_RET_CODE.equals("2007") || INFO_RET_CODE.equals("2008")) {
                            String bkBATCHID = tmpBkReader.getFirstText("/AIPG/QTRANSRSP/QTDETAIL/BATCHID");
                            String bkRET_CODE = tmpBkReader.getFirstText("/AIPG/QTRANSRSP/QTDETAIL/RET_CODE");
                            String bkERR_MSG = tmpBkReader.getFirstText("/AIPG/QTRANSRSP/QTDETAIL/ERR_MSG");
                            if (bkBATCHID != null && bkBATCHID.length() > 0 && !p_req9144.askNumber.equals(bkBATCHID)) {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "请求流水号" + p_req9144.askNumber + "与响应" + bkBATCHID + "不一致";
                            } else if (tmpStrRet[1].indexOf("QTDETAIL") < 0) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = INFO_ERR_MSG;
                                tmpRetDetail.reqSeqID = p_req9144.askNumber;
                                tmpRetDetail.transState = "H+" + INFO_RET_CODE;
                                tmpRetDetail.payInfoCode = INFO_RET_CODE;
                            } else {//1002	无此交易 表示最终失败
                                tmpRetDetail.respCode = "";
                                if (INFO_RET_CODE.equals("1002") || bkERR_MSG.contains("没有符合条件的记录") || bkERR_MSG.contains("记录不存在") || bkERR_MSG.contains("无此交易")) {
                                    canBeNotExist(p_req9144, tmpRetDetail, 50);
                                }
                                if (!tmpRetDetail.respCode.equals("-1")) {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.reqSeqID = p_req9144.askNumber;
                                    tmpRetDetail.transState = "H+" + INFO_RET_CODE;
                                    if (bkRET_CODE != null && bkRET_CODE.length() > 0) {
                                        tmpRetDetail.transState += "+D+" + bkRET_CODE;
                                        tmpRetDetail.payInfoCode = bkRET_CODE;
                                        if ("3999".equals(bkRET_CODE)) {
                                            tmpRetDetail.payInfoCode = bkERR_MSG;
                                        }
                                        tmpRetDetail.respInfo = bkERR_MSG;
                                    } else {
                                        tmpRetDetail.payInfoCode = INFO_RET_CODE;
                                        tmpRetDetail.respInfo = INFO_ERR_MSG;
                                    }
                                    String bkAMOUNT = tmpBkReader.getFirstText("/AIPG/QTRANSRSP/QTDETAIL/AMOUNT");
                                    if (bkAMOUNT != null && bkAMOUNT.length() > 0) {
                                        tmpRetDetail.transAmt = StringHelper.amountDivide100(bkAMOUNT);
                                    }
                                }
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = INFO_RET_CODE + "-" + INFO_ERR_MSG;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9144.recAct + "]对应的商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    public RetAtsSingle d_9144_9108JFB(ReqQrySingle p_req9144) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9144);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9144.recAct, "");
            if (actItem != null) {
                AllInPay02Header bankHeader = new AllInPay02Header();
                bankHeader.REQ_SN = p_req9144.askNumber;
                bankHeader.TRX_CODE = "200004";

                XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
                writeJFBCommonHeader(xmlWriter, bankHeader, actItem);
                xmlWriter.writeStartElement("BODY");
                xmlWriter.writeStartElement("QTRANSREQ");
                xmlWriter.writeElementString("SRC_BATCHID", p_req9144.askNumber);
                xmlWriter.writeElementString("SRC_SN", "");
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendJFBToBank(xmlWriter.toXmlString(), actItem, bankHeader.REQ_SN);

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String INFO_RET_CODE = tmpBkReader.getFirstText("/AIPG/HEAD/RET_CODE");
                        String INFO_ERR_MSG = tmpBkReader.getFirstText("/AIPG/HEAD/RET_MSG");
                        if ("0000".equals(INFO_RET_CODE)) {
                            String bkBATCHID = tmpBkReader.getFirstText("/AIPG/BODY/QTDETAILS/QTDETAIL/BATCHID");
                            String bkRET_CODE = tmpBkReader.getFirstText("/AIPG/BODY/QTDETAILS/QTDETAIL/RESULT_CODE");
                            String bkERR_MSG = tmpBkReader.getFirstText("/AIPG/BODY/QTDETAILS/QTDETAIL/RESULT_MSG");
                            if (bkBATCHID != null && bkBATCHID.length() > 0 && !p_req9144.askNumber.equals(bkBATCHID)) {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "请求流水号" + p_req9144.askNumber + "与响应流水号" + bkBATCHID + "不一致";
                                retAts.writeRetDetail(tmpRetDetail);
                                return retAts;
                            }
                            if (StringHelper.hasAnyChar(bkRET_CODE)) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.transState = "JH+" + INFO_RET_CODE + "+JD+" + bkRET_CODE;
                                tmpRetDetail.payInfoCode = bkRET_CODE;
                                if ("3999".equals(bkRET_CODE)) {
                                    tmpRetDetail.payInfoCode = bkERR_MSG;
                                }
                                tmpRetDetail.respInfo = bkERR_MSG;
                            } else {
                                tmpRetDetail.payInfoCode = "-1";
                                tmpRetDetail.respInfo = "通联金服宝响应报文/AIPG/BODY/QTDETAILS/QTDETAIL/RESULT_CODE节点为空，处理中，请稍后查询！";
                            }
                        } else if ("1002".equals(INFO_RET_CODE)) {
                            if (canBeNotExist(p_req9144, tmpRetDetail, 50)) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.reqSeqID = p_req9144.askNumber;
                                tmpRetDetail.transState = "JH+" + INFO_RET_CODE;
                                tmpRetDetail.payInfoCode = INFO_RET_CODE;
                                tmpRetDetail.respInfo = INFO_ERR_MSG;
                                tmpRetDetail.transAmt = p_req9144.payAmount;
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = INFO_RET_CODE + "-" + INFO_ERR_MSG;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9144.recAct + "]对应的商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}